<template>
	<el-form ref="dialogForm" label-width="100px">
		<el-row
			><el-form-item label-width="120px" label="方法列表"
				><el-select
					placeholder="---请选择请求的方法---"
					v-model="requestIndex"
					style="width: 300px"
					@change="optionChange"
				>
					<el-option
						v-for="(item, i) in optionsInfo"
						:key="item.index"
						:label="`${i + 1}.${item.indexText}`"
						:value="item.index"
					/> </el-select
				><el-icon v-if="hasToken" :size="20"> <key /> </el-icon
				><span v-if="!hasToken" style="color: red">尚未登录,请登录</span
				><button @click="dialogVisble = true" v-if="hasToken">
					查看token
				</button></el-form-item
			></el-row
		><el-row
			><el-form-item label-width="120px" label="参数说明"
				><el-input
					v-model="requestRemark"
					placeholder="参数说明"
					style="width: 1200px"
					rows="10"
					type="textarea" /></el-form-item></el-row
		><el-row
			><el-form-item label-width="120px" label="请求参数"
				><el-input
					v-model="requestContent"
					placeholder="请求参数"
					style="width: 1200px"
					rows="10"
					type="textarea" /></el-form-item
		></el-row>
		<el-row
			><el-form-item label-width="120px" label=""
				><el-button @click="submit">提交</el-button></el-form-item
			></el-row
		>
		<el-row
			><el-form-item label-width="120px" label="执行结果"
				><el-input
					v-model="requestResult"
					placeholder="执行结果"
					style="width: 1200px"
					rows="20"
					type="textarea" /></el-form-item></el-row
	></el-form>
	<el-dialog
		title="token"
		v-model="dialogVisble"
		width="30%"
		:before-close="close"
	>
		<span>{{ token }}</span>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="dialogVisble = false">关闭</el-button>
			</span>
		</template>
	</el-dialog>
</template>
<script>
import publicjs from "@/api/model/publicjs";
import config from "@/config";
import http from "@/utils/request";
export default {
	name: "waiterService",
	emits: ["success", "closed"],
	components: {
		publicjs,
	},
	data() {
		return {
			//表单数据
			dialogVisble: false,
			token: "",
			hasToken: false,
			requestIndex: "appLogin",
			requestRemark: "",
			requestContent: "",
			requestResult: "",
			optionsInfo: [
            {
					index: "appLogin",
					indexText: "服务员登录",
					requestRemark:
						"orgname:门店编码(先默认0004);username:登录人编码(先默认admin);password:登录密码(先默认123456)",
					requestContent:
						'{"orgname":"0004","username":"admin","password":"123456"}',
				},
				{
					index: "SendCode",
					indexText: "发送验证码",
					requestRemark: `PhoneNumbers:手机号码,Code:验证码,OrgCode:机构编码`,
					requestContent:
						'{"functionName":"SendCode","contentData":{"PhoneNumbers":"","Code":"123456","OrgCode":"0004"}}',
				},
				{
					index: "SendRoomNo",
					indexText: "预定短信",
					requestRemark: `PhoneNumbers:手机号码,name:用户昵称,date:预定时间,roomno:房台名称,remain:预留时间,tel:联系电话号码,OrgCode:机构编码`,
					requestContent:
						'{"functionName":"SendRoomNo","contentData":{"PhoneNumbers":"","name":"admin","date":"2024-03-12 16:00:00","roomno":"boss1","remain":"2024-03-12 20:00:00","tel":"13000000001","OrgCode":"0004"}}',
				},
				{
					index: "SendUnRoomNo",
					indexText: "取消预定短信",
					requestRemark: `PhoneNumbers:手机号码,name:用户昵称,time:取消预订时间,roomno:房台名称,OrgCode:机构编码`,
					requestContent:
						'{"functionName":"SendUnRoomNo","contentData":{"PhoneNumbers":"","name":"admin","time":"2024-03-12 16:00:00","roomno":"boss1","OrgCode":"0004"}}',
				},
				{
					index: "Turntable",
					indexText: "预定转台短信",
					requestRemark: `PhoneNumbers:手机号码,name:用户昵称,time:预订时间,roomno:房台名称,newtime:新预订时间,newroomno:新房台名称,remain:预留时间,OrgCode:机构编码`,
					requestContent:
						'{"functionName":"Turntable","contentData":{"PhoneNumbers":"","name":"张三","time":"2024/03/09 15:30","roomno":"A3","newtime":"2024/03/09 17:31","newroomno":"A5","remain":"2024/03/09 15:30","OrgCode":"0004"}}',
				},
				{
					index: "VipCard",
					indexText: "会员开卡短信",
					requestRemark: `PhoneNumbers:手机号码,name:用户昵称,time:开卡时间,branchname:门店名称,money:充值金额,balance:余额,giftmoney:赠送金币,OrgCode:机构编码`,
					requestContent:
						'{"functionName":"VipCard","contentData":{"PhoneNumbers":"","name":"张三","time":"2024/03/09 15:30","branchname":"梦田酒馆（测试店）","money":"1200","balance":"1200","giftmoney":"50","OrgCode":"0004"}}',
				},
				{
					index: "VipRecharge",
					indexText: "会员充值短信",
					requestRemark: `PhoneNumbers:手机号码,name:用户昵称,time:充值时间,branchname:门店名称,recharge:充值金额,balance:余额,giftmoney:金币余额,OrgCode:机构编码`,
					requestContent:
						'{"functionName":"VipRecharge","contentData":{"PhoneNumbers":"","name":"张三","time":"2024/03/09 15:30","branchname":"梦田酒馆（测试店）","recharge":"1200","balance":"1200","giftmoney":"50","OrgCode":"0004"}}',
				},
				{
					index: "VipConsumption",
					indexText: "会员消费短信",
					requestRemark: `PhoneNumbers:手机号码,name:用户昵称,time:消费时间,branchname:门店名称,consume:消费金额,balance:余额,giftmoney:金币余额,OrgCode:机构编码`,
					requestContent:
						'{"functionName":"VipConsumption","contentData":{"PhoneNumbers":"","name":"张三","time":"2024/03/09 15:30","branchname":"梦田酒馆（测试店）","consume":"1200","balance":"0","giftmoney":"50","OrgCode":"0004"}}',
				},
				{
					index: "TakeWine",
					indexText: "取酒验证码",
					requestRemark: `PhoneNumbers:手机号码,code:验证码,OrgCode:机构编码`,
					requestContent:
						'{"functionName":"TakeWine","contentData":{"PhoneNumbers":"","code":"202403","OrgCode":"0004"}}',
				},
				{
					index: "SendCouponPush",
					indexText: "券推送",
					requestRemark: `PhoneNumbers:手机号码,MemberName:用户昵称,OrgName:门店名称,GoodsName:商品名称,PushNum:推送数量,Maturity:有效期,Remarks:活动详情,OrgCode:机构编码`,
					requestContent:
						'{"functionName":"SendCouponPush","contentData":{"PhoneNumbers":"","MemberName":"张三","OrgName":"梦田酒馆（测试店）","GoodsName":"梦田酒","PushNum":"1","Maturity":"2024/03/09","Remarks":"消费满100元，可抵扣一张果盘券","OrgCode":"0004"}}',
				},
				{
					index: "SendBookingDeadline",
					indexText: "预定临期短信",
					requestRemark: `PhoneNumbers:手机号码,name:用户昵称,shoptext:门店名称,date:预订日期,roomno:台房名称,remain:预留时间,time:剩余时间,tel:联系电话,OrgCode:机构编码`,
					requestContent:
						'{"functionName":"SendBookingDeadline","contentData":{"PhoneNumbers":"","name":"张三","shoptext":"梦田榕城店","date":"2024/03/08","roomno":"A6","remain":"17:30","time":"30","tel":"0751-88888888","OrgCode":"0004"}}',
				},
				{
					index: "SendUnsubscribe",
					indexText: "退订短信(收费)",
					requestRemark: `PhoneNumbers:手机号码,name:用户昵称,storename:门店名称,roomno:台房名称,time:预订时间,percent:违约金,money:余额,OrgCode:机构编码`,
					requestContent:
						'{"functionName":"SendUnsubscribe","contentData":{"PhoneNumbers":"","name":"张三","storename":"梦田榕城店","roomno":"A3","time":"20240308 18:30","percent":"100","money":"1200","OrgCode":"0004"}}',
				},
				{
					index: "SendWineExpiration",
					indexText: "存酒到期提醒",
					requestRemark: `PhoneNumbers:手机号码,branchname:门店名称,storecontent:酒水名称,OrgCode:机构编码`,
					requestContent:
						'{"functionName":"SendWineExpiration","contentData":{"PhoneNumbers":"","branchname":"梦田榕城店","storecontent":"百威一打","OrgCode":"0004"}}',
				},
				{
					index: "SendRedeem",
					indexText: "积分兑换",
					requestRemark: `PhoneNumbers:手机号码,name:用户昵称,branchname:门店名称,goodsname:商品名称,operScore:消耗积分,currentScore:剩余积分,OrgCode:机构编码`,
					requestContent:
						'{"functionName":"SendRedeem","contentData":{"PhoneNumbers":"","name":"张三","branchname":"梦田榕城店","goodsname":"百威一打","operScore":"1000","currentScore":"0","OrgCode":"0004"}}',
				},
				{
					index: "SendStoringWine",
					indexText: "存酒短信",
					requestRemark: `PhoneNumbers:手机号码,branchname:门店名称,storecontent:存酒名称,keepday:有效天数,MessageCode:取酒码,OrgCode:机构编码`,
					requestContent:
						'{"functionName":"SendStoringWine","contentData":{"PhoneNumbers":"","branchname":"梦田榕城店","storecontent":"百威一打","keepday":"15","MessageCode":"202413","OrgCode":"0004"}}',
				},
				{
					index: "SendTakeWine",
					indexText: "取酒短信",
					requestRemark: `PhoneNumbers:手机号码,branchname:门店名称,storecontent:酒水名称,takedate:取酒时间,OrgCode:机构编码`,
					requestContent:
						'{"functionName":"SendTakeWine","contentData":{"PhoneNumbers":"","branchname":"梦田榕城店","storecontent":"百威一打","takedate":"2024/03/11","OrgCode":"0004"}}',
				}
			],
		};
	},
	methods: {
		optionChange() {
			let option = this.optionsInfo.find(
				(item) => item.index == this.requestIndex
			);
			this.requestRemark = option.requestRemark;
			this.requestContent = option.requestContent;
			this.requestResult = "";
		},
		copyToken() {
			this.requestContent = this.token;
		},
		async submit() {
			if (this.requestIndex == "appLogin") {
				let url = `${config.API_URL}/AppLogin`;
				let data = JSON.parse(this.requestContent);
				let result = await http.get(url, data);
				if (result.opFlag == "1") {
					this.token = this.requestResult = result.userInfo.token;
					this.hasToken = true;
					this.requestResult = "登录成功,token:" + this.token;
				} else {
					this.requestResult = result.opMsg;
					this.hasToken = false;
				}
			} else if (this.requestIndex == "attachment") {
				//
			} else {
				if (!this.hasToken) {
					this.requestResult = "请登录";
				} else {
					let url = `${config.API_URL}/AppObjProcess`;
					let data = JSON.parse(this.requestContent);
					let result = await http.post(url, data, {
						headers: {
							token: "Bearer " + this.token,
						},
					});
					if (result.opFlag == "1") {
						// eslint-disable-next-line no-debugger
						//debugger;
						try {
							this.requestResult = JSON.stringify(
								JSON.parse(
									JSON.stringify(result.content)
										.replaceAll('\\"', '"')
										.replaceAll('"[', "[")
										.replaceAll(']"', "]")
								)
							);
						} catch (error) {
							this.requestResult =
								result.content.objectList[0].rowData;
						}
					} else {
						this.requestResult = result.opMsg;
					}
				}
			}
		},
	},
	async mounted() {
		this.optionChange();
	},
};
</script>
